package com.atguigu.test.boot.bean;

import com.atguigu.test.boot.annotation.MyConfigurationProperties;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义的DispatcherServlet对象
 * @date 2023/2/7 19:41
 */
public class MyDispatcherServlet extends DispatcherServlet implements Serializable{

    /**
     * @description:  配置自定义的环境变量注册器
     * @author zp
     * @date: 2023/2/7 20:00
     * @return ConfigurableEnvironment
     */
    @Override
    public ConfigurableEnvironment getEnvironment() {
        return new MyStandardServletEnvironment();
    }

//    @Override
//    protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
//
//        super.configureAndRefreshWebApplicationContext(wac);
//        String className;
//        String pre;
//        ConfigurableEnvironment environment = wac.getEnvironment();
//        // 配置原数据读取器
//        MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory();
//        try {
//            Resource[] resources = wac.getResources("classpath*:com/atguigu/test/boot/**/*.class");
//            for (Resource resource : resources) {
//                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
//                className = metadataReader.getClassMetadata().getClassName();
//                Class<?> aClass = Class.forName(className);
//                if(aClass.isAnnotationPresent(MyConfigurationProperties.class)){
//                    className = className.substring(className.lastIndexOf(".")+1);
//                    char indexChar = (char) (className.charAt(0)+32);
//                    className = className.substring(1);
//                    className = indexChar+className;
//                    MyConfigurationProperties declaredAnnotation = aClass.getDeclaredAnnotation(MyConfigurationProperties.class);
//                    pre = declaredAnnotation.pre();
//                    Object obj = aClass.newInstance();
//                    Field[] fields = obj.getClass().getDeclaredFields();
//                    for (Field field : fields) {
//                        field.setAccessible(true);
//                        field.set(obj,environment.getProperty(pre+field.getName()));
//                    }
//                    //wac.getBeanFactory().registerSingleton(className,obj);
//                }
//            }
//            System.out.println("打印所有的class："+ Arrays.toString(resources));
//        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
